package 请求客户端;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2024-12-16
 * Time: 16:37
 */
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class SimpleHttpServer {
    public static void main(String[] args) throws Exception {
        // 创建一个HTTP服务器，监听8080端口
        HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
        server.createContext("/OOPCache/exampleGroup/exampleKey", new MyHandler());
        server.setExecutor(null); // 使用默认执行器
        server.start();
        System.out.println("Server started on port 8080. Press Ctrl+C to stop.");
    }

    static class MyHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange t) throws IOException {
            String response = "Hello, this is the data for exampleGroup and exampleKey.";
            t.getResponseHeaders().add("Content-Type", "text/plain");
            t.sendResponseHeaders(200, response.length()); // 设置响应码和内容长度
            OutputStream os = t.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }
}
